package com.banana.manager.forest.controller.chemical;

import com.banana.manager.forest.dao.chemical.IOutDao;
import com.banana.manager.forest.entity.chemical.DeviceBean;
import com.banana.manager.forest.entity.chemical.OutBean;
import com.banana.manager.forest.entity.chemical.OutChose;
import com.banana.manager.forest.service.impl.chemical.DeviceServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/out")
public class OutController {
    @Resource
    private IOutDao outDao;

    private boolean flag;

    @Resource
    private DeviceServiceImpl deviceService;

    /**
     * 采用购物车的原理，并没有添加到数据库
     * outChose是选择的器械
     * 如果选择了重复的器械，就在原器械数量上+1并保存session
     * @param request
     * @param
     * @return
     */
    @PostMapping("/addOutCart")
    public List<OutBean> addNewOut(HttpServletRequest request, @RequestBody List<DeviceBean> deviceList){
        flag = false;
        System.out.println("++++++++++++++++++++++++++++++++++++++");
        HttpSession session = request.getSession();
        List<OutBean> outList = (List<OutBean>) session.getAttribute("outList");
        if(outList == null){
            outList = new ArrayList<>();
        }
        for(int i = 0; i<deviceList.size();i++){
            for (OutBean bean : outList) {
                if (Objects.equals(bean.getDeviceBean().getDeviceId(), deviceList.get(i))) {
                    bean.setOutAmount(bean.getOutAmount() + 1);
                    flag=true;
                    break;
                }
            }

            if(flag){
                flag=false;
            }else {
                OutBean outBean = new OutBean();
                outBean.setDeviceBean(deviceList.get(i));
                outBean.setOutAmount(10);
                outList.add(outBean);
            }

        }
        session.setAttribute("outList",outList);
        System.out.println("测试看是否在session 里"+session.getAttribute("outList"));
        return outList;
    }

    /**
     * 删除购物车中的数据
     * @param request
     * @param deviceId
     */
    @GetMapping("/deleteOut")
    public List<OutBean> deleteOut(HttpServletRequest request, Integer deviceId){
        HttpSession session = request.getSession();
        List<OutBean> outList = (List<OutBean>) session.getAttribute("outList");
        System.out.println("这里测试是否成功提取到session"+session.getAttribute("outList"));
        for(OutBean bean : outList){
            if(bean.getDeviceBean().getDeviceId().equals(deviceId)){
                outList.remove(bean);
            }
        }
        session.setAttribute("outList",outList);
        return outList;
    }

    @GetMapping("/getAllOut")
    public List<OutBean> getAllOut(HttpServletRequest request){
        HttpSession session = request.getSession();
        return (List<OutBean>) session.getAttribute("outList");
    }
}
